<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Role_Entity extends CI_Model
{
    private $tablename = 'role';
    private $rid = null;
    private $name = null;
    private $resource = null;
    
    public function setRid($Rid)
    {
        $this->rid = $Rid;
    }

    public function setName($Name)
    {
        $this->name = $Name;
    }

    public function setResource($Resource)
    {
        $this->resource = $Resource;
    }

    public function save()
    {
        $params = array();
        foreach ($this as $key => $val) {
            if ($key == 'tablename') continue;
            if ($val === null || $val === '') continue;
            $params[$key] = $val;
        }
                
        if (!isset($params['id'])) {
            if ($this->db->insert($this->tablename, $params)) {
                return $this->db->insert_id();
            } else {
                return 0;
            }
        } else {
            return $this->db->update($this->tablename, $params, array('id' => $params['id']));
        }
        
    }
    
    public function del($id)
    {
        
        return $this->db->delete($this->tablename, array('id' => $id));    
    }
    
    public function getRoleById($id)
    {
        $query = $this->db->get_where($this->tablename, array('id' => $id));
        if ($query->num_rows() > 0) {
            return $query->row();
        }
        return false;
    }
    
    public function getAllList()
    {
        $query = $this->db->get($this->tablename);
        return $query->result();
    }
    
    public function TotalRole()
    {
        $query = $this->db->count_all_results('Role'); 
        return $query->result();
    } 
}       
        